package cn.golxen.rsms.service.impl;

import cn.golxen.rsms.bean.bo.*;
import cn.golxen.rsms.bean.entity.GoodsLog;
import cn.golxen.rsms.bean.entity.GoodsPurchase;
import cn.golxen.rsms.bean.entity.GoodsStore;
import cn.golxen.rsms.bean.entity.PayLog;
import cn.golxen.rsms.commons.utils.LocalDateTimeUtils;
import cn.golxen.rsms.mapper.LogMapper;
import cn.golxen.rsms.service.LogService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    LogMapper logMapper;

    @Override
    public int addGoodsLog(GoodsLog goodsLog) {
        return logMapper.addGoodsLog(goodsLog);
    }

    @Override
    public int addGoodsLogList(List<GoodsLog> goodsLogs) {
        return logMapper.addGoodsLogList(goodsLogs);
    }

    @Override
    public int addGoodsLogListByObject(Object object,String type) {
        List<GoodsLog> goodsLogs=new LinkedList<>();
        List<AddOrOutGoodsNumBo> addOrOutGoodsNumBoList=new LinkedList<>();
        GoodsPurchase goodsPurchase;
        List<GoodsStore> goodsStoreList;
        PayLog payLog;


        if (Objects.equals(type, "一键入库")){
            goodsPurchase = (GoodsPurchase) object;
            addOrOutGoodsNumBoList=goodsPurchase.getOrderDetailObject();
        }
        else if (Objects.equals(type, "新建商品入库")){
            goodsStoreList=(List<GoodsStore>) object;
            for (GoodsStore goodsStore:goodsStoreList){
                if (goodsStore.getInventory()==0)
                    continue;
                AddOrOutGoodsNumBo addOrOutGoodsNumBo1=new AddOrOutGoodsNumBo();
                addOrOutGoodsNumBo1.setNum(goodsStore.getInventory());
                BeanUtils.copyProperties(goodsStore,addOrOutGoodsNumBo1);
                addOrOutGoodsNumBoList.add(addOrOutGoodsNumBo1);
            }
        }else if(Objects.equals(type,"售出")){
            payLog= (PayLog) object;
            if (payLog.getIsDone()==1){
                addOrOutGoodsNumBoList= payLog.getDetailObject();
            }

        }
        else
            addOrOutGoodsNumBoList = (List<AddOrOutGoodsNumBo>) object;


        if (addOrOutGoodsNumBoList!=null&&addOrOutGoodsNumBoList.size()>0){
            for (AddOrOutGoodsNumBo addOrOutGoodsNumBo : addOrOutGoodsNumBoList) {
                GoodsLog goodsLog = new GoodsLog();
                BeanUtils.copyProperties(addOrOutGoodsNumBo,goodsLog);

                if(type.contains("入库"))
                    goodsLog.setIsGet(1);
                if(type.contains("出库")||type.equals("售出"))
                    goodsLog.setIsGet(0);
                goodsLog.setCreateDate(LocalDateTimeUtils.getLocalDateTime());
                goodsLog.setRemarks(type);
                goodsLogs.add(goodsLog);
            }

            return logMapper.addGoodsLogList(goodsLogs);
        }

        return 0;
    }

    @Override
    public List<GoodsLog> getGoodsLogByPeriod(GetGoodsLogByPeriodBo getGoodsLogByPeriodBo) {
        return logMapper.getGoodsLogByPeriod(getGoodsLogByPeriodBo);
    }

    @Override
    public List<GetGoodsWarnStateBo> getGoodsSalesDaysByLogDayAndCode(int day, List<String> codeList, int storeId) {
        return logMapper.getGoodsSalesDaysByLogDayAndCode(day,codeList,storeId);
    }

    @Override
    public int updateOrderIsDone(String orderId, String customerId, int isDone) {
        return logMapper.updateOrderIsDone(orderId,customerId,isDone);
    }

    @Override
    public int addPayLog(PayLog payLog) {
        payLog.setDetailStr(JSONObject.toJSONString(payLog.getDetailObject()));
        return logMapper.addPayLog(payLog);
    }

    @Override
    public List<PayLog> getPayLogListByPeriod(BusinessSalesFlowBo bo) {

        List<PayLog> payLogs=logMapper.getPayLogListByPeriod(bo);
        for (PayLog payLog:payLogs){
            payLog.setDetailObject(new ArrayList<AddOrOutGoodsNumBo>(JSONObject.parseArray(payLog.getDetailStr(), AddOrOutGoodsNumBo.class)));
        }
        return payLogs;
    }

    @Override
    public List<PayLog> getDonePayLogListByPeriod(LocalDateTime startDate, LocalDateTime endDate, int storeId) {
        List<PayLog> payLogs=logMapper.getDonePayLogListByPeriod(startDate,endDate,storeId);
        for (PayLog payLog:payLogs){
            payLog.setDetailObject(new ArrayList<AddOrOutGoodsNumBo>(JSONObject.parseArray(payLog.getDetailStr(), AddOrOutGoodsNumBo.class)));
        }
        return payLogs;
    }

    @Override
    public List<GoodsLog> getSalesGoodsLogByPeriod(BusinessDateBo bo) {
        return logMapper.getSalesGoodsLogByPeriod(bo);
    }

    @Override
    public List<GoodsSalesTypeNumBo> getGoodsSalesTypeNumByDay(LocalDateTime startDate,LocalDateTime endDate ,int storeId) {
        return logMapper.getGoodsSalesTypeNumByDay(startDate,endDate,storeId);
    }

    @Override
    public PayLog getLastOrderByDone(int storeId) {
        PayLog payLog=logMapper.getLastOrderByDone(storeId);
        payLog.setDetailObject(new ArrayList<AddOrOutGoodsNumBo>(JSONObject.parseArray(payLog.getDetailStr(), AddOrOutGoodsNumBo.class)));
        return payLog;
    }


}
